var params = {};
var defaultPageSize = 10;
var currentPage = 1;
var pageSize = defaultPageSize;


$(function(){
	//queryNextParams("DCLD");
	loadParams();
	$('.check').on('click',search);
	$('.empty').on('click',emptySearch);
	//新增
	$('.add').on('click',addSysParams);
});

function addSysParams(){
	$('input[name=typeCode]').removeAttr("readonly");
	var dialog_info = {};
	dialog_info.title = '新增系统参数';
	dialog_info.yesbtn = '保存';
	clearForm();
	$("#paramLevel").val("0");
	saveOrupdateParams(dialog_info);
}

function loadParams(){
	params.currentPage = currentPage;
	params.pageSize = pageSize;
	$.ajax({
		url: webUrl+'sysParams/getSysParamsList.do',
		type:'post',
		data:params
	}).done(function(data){
		var result = data.bo;
		var pager = {};
		$('#sysParams').datagrid({
			fitColumns:true,
			singleSelect:true,
			pagination:true,
			remoteSort:false,
			multiSort:true,
			loadMsg:"数据加载中，请稍后……",
			data:result.list,
			columns:[[
			          /*{field:'ck',checkbox:true},*/
			          /*{field:'id',title:'id',sortable:true},*/
			          {field:'_itemNo',title:'序号',align:'center',width:10,formatter:formatId},
			          {field:'type_code',title:'类型编码',width:50,sortable:true},
			          {field:'param_value',title:'参数值',width:60,sortable:true},
			          {field:'param_level',title:'参数层级',sortable:true},
			          {field:'status',title:'状态标识',sortable:true},
			          {field:'last_updated_date',title:'更新时间',sortable:true,formatter:formatTime},
			          {field:'_operate',title:'操作',formatter:formatOper},
			      ]],
			      onSortColumn: function (sort, order) {
			    	  setPager(pager,result.total);
         }
		});
		var pager = $('#sysParams').datagrid().datagrid('getPager');	// get the pager of datagrid
		setPager(pager,result.total);
		
		//删除菜单
		$('.delicon').on('click',deleteSysparam);
		$('.dealicon').on('click',editGuide);
		$('.playicon').on('click',onchange);
		$('.addicon').on('click',addNextParam);
	});
}

function addNextParam(){
	var id = $(this).attr('row-id');
	//alert(obj);
	localStorage.setItem("rowId", id);
	$('.main_panel').load(webUrl+"public/page/sysParams/addNextParams.html");
}

function onchange(){
	var id = $(this).attr('row-id');
	localStorage.setItem("rowId", id);
	$('.main_panel').load(webUrl+"public/page/sysParams/nextParams.html");
	
	// var ul=document.getElementById("projecthome");   
     /*var obj=document.createElement("li");         
     obj.innerHTML="新增下一级";           
     ul.appendChild(obj);   */
}

function deleteSysparam(){
	var id = $(this).attr('row-id');
	var tips = '确认是否删除?';
	deleteTips(id,tips);
}

function deleteTips(id,tips){
	$("#tipsInfo").html('<b class="sighicon"></b>'+tips);
		new Dialog({
			target:'#tipsbox',
			width:300,
			height:160,
			showYesBtn:true,
			showNoBtn:true,
			yesFn:function(){
				deleteSysParamData(id);
			},
			noFn:function(){}
			}).show();
}

function deleteSysParamData(id){
	$.ajax({
		url:webUrl+'sysParams/deleteSysParamById.do',
		type:'post',
		data:{id:id}
	}).done(function(data){
		loadParams();
	});
}

function editGuide(){
	$('input[name=typeCode]').attr("readonly","readonly");
	$('input[name=paramLevel]').attr("readonly","readonly");
	var id = $(this).attr('row-id');
	var dialog_info = {};
	dialog_info.title = '修改系统参数';
	dialog_info.yesbtn = '保存';
	dialog_info.id=id;
	saveOrupdateParams(dialog_info,id);
	getSysParamsById(id);
}

function saveOrupdateParams(dialog_info){
	new Dialog({
		target:'#dialogboxparams',
		width:800,
		height:650,
		showYesBtn:true,
		showNoBtn:true,
		title: dialog_info.title,
		yesBtnText:dialog_info.yesbtn,
		noBtnText:'取消',
		yesFn:function(){saveSysParams(this,dialog_info.id)},
		noFn:function(){}}).show();
}

function saveSysParams(obj,id){
	var json={};
    json = $("#addDataFormParam").serializeObject();
    //alert(JSON.stringify(json));
    if(checknull("paramValue")){
    		obj.boolConfirm = true;
    		if(id!=null){
    			updateSysParams(json);
    			clearForm();
    		}else if(checknull("typeCode")){
    			addEntitySysParam(json);
    			clearForm(); 
    		}
    }else{
    	obj.boolConfirm = false;
    }
}

function addEntitySysParam(obj){
	obj=JSON.stringify(obj);
	$.ajax({
		url: webUrl+'sysParams/addEntitySysParam.do',
		type:'post',
		data:{param:obj}
	}).done(function(data){
		loadParams();
	});
}

function updateSysParams(obj){
	obj=JSON.stringify(obj);
	$.ajax({
		url: webUrl+'sysParams/updateSysParams.do',
		type:'post',
		data:{param:obj}
	}).done(function(data){
		loadParams();
	});
}

function getSysParamsById(id){
	$.ajax({
		url:webUrl+'sysParams/getSysParamsById.do',
		type:'post',
		data:{id:id}
	}).done(function(data){
		var aa=eval("("+data.bo+")");
		var result=aa;
		$("#id").val(id);
		$("#typeCode").val(result.typeCode);
		$("#paramValue").val(result.paramValue);
		$("#paramLevel").val(result.paramLevel);
		$("input[name='status'][value="+result.status+"]").attr("checked",true);
	});
}

var formatId = function(val, row,index){
	return (index+1)+(currentPage-1)*pageSize;
}

var formatTime = function(val){
	var unixTimestamp = new Date(val);  
    return unixTimestamp.toLocaleString();
}

var formatOper = function(val,row,index){
	return '<a href="#" class="playicon  ml10" row-id="'+row.id+'" data-tipso="下一级"></a>  <a href="#"  class="addicon  ml10" data-tipso="新增下一级" row-id="'+row.id+'"></a>  <a href="#" class="dealicon  ml10"  row-id="'+row.id+'" data-tipso="编辑"></a> <a href="#;"  class="delicon  ml10" row-id="'+row.id+'" data-tipso="删除"></a>';
}

function setPager(pager,total){
	pager.pagination({
		total:total,
		pageNumber:currentPage,
	    pageSize:defaultPageSize,
	    layout:['first','prev','links','next','last'],
	    onSelectPage:function(cpage, psize){
	    	currentPage=cpage;
	    	pageSize=psize;
	    	loadParams();
		}
	});
}

function search(){
	var keywords = $('#keywords').val();
	if(keywords!='')
	{
		params.typeCode = keywords;
		loadParams();
	}
}

function emptySearch(){
	$('#keywords').val('');
	params = {};
	loadParams();
}

function clearForm(){
	clearValue("typeCode");
	clearValue("paramValue");
	clearValue("paramLevel");
	//$("input[name='sex'][value=Y]").attr("checked",true);
}

function clearValue(){
	for (var i = 0; i < arguments.length; i++) {
		var obj = document.getElementById(arguments[i]);
		obj.value='';
	}
}

$.fn.serializeObject = function()    
{    
   var o = {};    
   var a = this.serializeArray();    
   $.each(a, function() {    
       if (o[this.name]) {    
           if (!o[this.name].push) {    
               o[this.name] = [o[this.name]];    
           }    
           o[this.name].push(this.value || '');    
       } else {    
           o[this.name] = this.value || '';    
       }    
   });    
   return o;    
};  